package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.*;
import com.example.chatchatbackend.entity.dto.Products;
import com.example.chatchatbackend.entity.dto.Products;
import com.example.chatchatbackend.entity.vo.response.ProductsVO;
import com.example.chatchatbackend.entity.vo.response.ProductsVO;
import com.example.chatchatbackend.entity.vo.response.ProductsVO;
import com.example.chatchatbackend.mapper.ProductsMapper;
import com.example.chatchatbackend.service.ProductsService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();
    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public List<Products> getAllProducts() {
        return productsMapper.getAll();
    }


    @Override
    public ProductsVO insertProducts(ProductsVO productsVO) {
        Products products = new Products(null,productsVO.getName(),productsVO.getDescription(),productsVO.getPrice(),productsVO.getCategoryId(),this.currentTime,this.currentTime,productsVO.getImageUrl(),productsVO.getCategoryName());
        if(this.save(products))
            return productsVO;
        return null;
    }

    @Override
    public List<ProductsVO> insertProductsList(List<ProductsVO> productsVOList) {

        List<Products> productsList = new ArrayList<>();

        for (ProductsVO productsVO : productsVOList) {
            productsList.add(new Products(null,productsVO.getName(),productsVO.getDescription(),productsVO.getPrice(),productsVO.getCategoryId(),this.currentTime,this.currentTime,productsVO.getImageUrl(),productsVO.getCategoryName()));
        }
        productsMapper.insertList(productsList);
        return productsVOList;
    }

    @Override
    public IPage<Products> pageThree(Integer pageNum, Integer pageSize) {
        Page<Products> page = new Page<>(pageNum, pageSize);
        IPage<Products> productsIPage = productsMapper.selectPage(page, null);
        return productsIPage;
    }

    @Override
    public ProductsVO updateProducts(ProductsVO productVO) {
        Products products = new Products();
        BeanUtils.copyProperties(productVO, products);
        products.setUpdatedTime(this.currentTime);
        productsMapper.updateProduct(products);
        return productVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        productsMapper.deleteBatchIds(ids);
    }

    @Override
    public List<ProductsVO> findByIds(List<Integer> ids) {
        List<Products> productsList = productsMapper.selectBatchIds(ids);
        List<ProductsVO> productsVOList = new ArrayList<>();

        for (Products products : productsList) {
            ProductsVO productsVO = new ProductsVO();
            BeanUtils.copyProperties(products, productsVO);
            productsVOList.add(productsVO);  // 添加到结果列表
        }

        return productsVOList;
    }
}
